﻿using Microsoft.Practices.Unity;
using System;

namespace SelfHosting
{
    /// <summary>
    /// Fournisseur de services
    /// </summary>
    public class ServiceProvider : IServiceProvider
    {
        #region Constructeur

        /// <summary>
        /// Initialise une nouvelle instance de la classe ServiceProvider
        /// </summary>
        /// <param name="unityContainer"></param>
        public ServiceProvider(IUnityContainer unityContainer)
        {
            UnityContainer = unityContainer;
        }

        #endregion

        #region Propriétés

        private IUnityContainer UnityContainer { get; set; }

        #endregion

        #region Méthodes

        /// <summary>
        /// Obtient une instance d'un service
        /// </summary>
        /// <param name="serviceType">type de service</param>
        /// <returns>une instance du service</returns>
        public object GetService(Type serviceType)
        {
            if (UnityContainer.IsRegistered(serviceType))
            {
                return UnityContainer.Resolve(serviceType);
            }
            return null;
        }

        #endregion
    }
}
